package classAndStudent;

import java.util.Arrays;

class Classroom {
    private final String className;
    private final Student[] students; // 存放5个学生

    public Classroom(String className, Student[] students) {
        this.className = className;
        this.students = students;
    }

    public String getClassName() {
        return className;
    }

    public void printCourseAverages() {
        double[] courseTotals = new double[6];
        for (Student student : students) {
            double[] scores = student.getScores();
            for (int i = 0; i < scores.length; i++) {
                courseTotals[i] += scores[i];
            }
        }
        System.out.println("每门课的平均成绩：");
        for (int i = 0; i < courseTotals.length; i++) {
            System.out.printf("课程%d: %.2f\n", i + 1, courseTotals[i] / students.length);
        }
    }

    public void printBestAndWorstStudents() {
        Student bestStudent = students[0];
        Student worstStudent = students[0];
        for (Student student : students) {
            if (student.getTotalScore() > bestStudent.getTotalScore()) {
                bestStudent = student;
            }
            if (student.getTotalScore() < worstStudent.getTotalScore()) {
                worstStudent = student;
            }
        }
        System.out.println("成绩最好的学生信息：" + bestStudent.getName() + ", 总成绩: " + bestStudent.getTotalScore());
        System.out.println("成绩最差的学生信息：" + worstStudent.getName() + ", 总成绩: " + worstStudent.getTotalScore());
    }

    public void sortStudentsByTotalScore() {
        Arrays.sort(students, new java.util.Comparator<>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (int) o2.getTotalScore() - (int) o1.getTotalScore();
            }
        });
        System.out.println("按总成绩降序排列的学生：");
        for (Student student : students) {
            System.out.println(student.getName() + ": 总成绩 " + student.getTotalScore());
        }
    }
}